package com.codelife.one.serizlizable;

import java.io.*;

/**
 * @ClassName Demo2
 * @Description TODO
 * @Date 2019/10/22 16:45
 * @Version 1.0
 **/
public class Demo2 implements Serializable {

    private String name;

    private Student student;

    @Override
    public String toString() {
        return "Demo2{" +
                "name='" + name + '\'' +
                ", student=" + student +
                '}';
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object deepClone() throws Exception{//将对象写到流里
        ByteArrayOutputStream by1 = new ByteArrayOutputStream();
        ObjectOutputStream ob1 = new ObjectOutputStream(by1);
        ob1.writeObject(this);

        ByteArrayInputStream by2 = new ByteArrayInputStream(by1.toByteArray());
        ObjectInputStream ob2 = new ObjectInputStream(by2);
        return(ob2.readObject());
    }
}
